package cn.edu.nchu.fragmentdemo.db.helper;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;

import cn.edu.nchu.fragmentdemo.R;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author Tsuki
 */

public class ORMLiteHelper extends OrmLiteSqliteOpenHelper {

    private static final String mTag = "TAG";
    public static final int MAX_INIT_FILE_SIZE = 2048;
    private static Context context;
    private static ORMLiteHelper instance;
    public static final String DATABASE_DIRECTORY = "AdvanceTactics";
    public static final String DATABASE_NAME = "AdvanceTactics.db";
    public static final int DATABASE_VERSION = 1;

    /**
     * 存储APP中所有的DAO对象的Map集合
     */
    private final Map<String, Dao> daos = new HashMap<>();



    public ORMLiteHelper(Context mcontext) {
        super(mcontext, getDatabasePath(mcontext), null, DATABASE_VERSION);
        context = mcontext;
    }

    private static String getDatabasePath(Context context) {
        File databaseDir = new File(context.getExternalFilesDir(null), DATABASE_DIRECTORY);
        if (!databaseDir.exists()) {
            databaseDir.mkdirs();
        }
        return new File(databaseDir, DATABASE_NAME).getAbsolutePath();
    }


    public static ORMLiteHelper getInstance(Context context) {
        if (instance == null) {
            synchronized (ORMLiteHelper.class) {
                if (instance == null) {
                    instance = new ORMLiteHelper(context);
                }
            }
        }
        return instance;
    }

    @Override
    public synchronized Dao getDao(Class clazz) throws SQLException, java.sql.SQLException {
        Dao dao = null;
        String className = clazz.getSimpleName();
        if (daos.containsKey(className)) {
            dao = daos.get(className);
        }
        if (dao == null) {
            dao = super.getDao(clazz);
            daos.put(className, dao);
        }
        return dao;
    }


    /**
     * @return
     */
    public static String[] initDatabaseSQL() {
        Log.d(mTag, "导入数据库");
        String[] strs = null;
        BufferedReader br = null;
        try {
            InputStream io = context.getResources().openRawResource(R.raw.init);
            br = new BufferedReader(new InputStreamReader(io));
            char[] buffer = new char[io.available()];
            br.read(buffer);
            strs = String.valueOf(buffer).split(";");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        for (int i = 0; i < Objects.requireNonNull(strs).length - 1; i++) {
            Log.d(mTag, i + "-->" + strs[i]);
        }
        return strs;
    }



    @Override
    public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
        // TODO Auto-generated method stub
        Log.d(mTag, "数据库初始化");
        String[] sqls;
        if ((sqls = initDatabaseSQL()) != null) {
            for (int i = 0; i < sqls.length - 1; i++) {
                Log.d(mTag, i + "-->" + sqls[i]);
                arg0.execSQL(sqls[i]);
            }
            Log.d(mTag, "初始化完毕");
        }
    }



    @Override
    public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub

    }




}
